import 'dart:convert';
import 'package:study_online/entity/CourseScore.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_echart/flutter_echart.dart';
import 'package:flutter_screenutil/screen_util.dart';

import 'package:flutter/material.dart';
import 'package:study_online/entity/Remark.dart';
import 'package:study_online/global/colors.dart';
import 'package:study_online/pages/studyPage/views/RemarkItem.dart';

import '../StudyModel.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        //appBar: AppBar(
        //  backgroundColor: Colors.black26,
        //  title: Text('container'),
        //),
        body: CourseRemarkTab(),
      ),
    );
  }
}

class CourseRemarkTab extends StatefulWidget {
  CourseRemarkTab({Key key,this.couId}) : super(key: key);
  int couId;
  @override
  _CourseRemarkTabState createState() {
    return _CourseRemarkTabState();
  }
}

class _CourseRemarkTabState extends State<CourseRemarkTab> {
  List<EChartPieBean> _dataList;
  List<int> _list = [1, 0, 0, 0, 0];
  int _selectedIndex = 0, couId = 1;
  RemarkList _remarkList = new RemarkList([]);
  RemarkList _tempRemarkList=new RemarkList([]);
  CourseScore _courseScore;
  @override
  void initState() {
    super.initState();
    _loadData(couId, 1);
  }

  @override
  void dispose() {
    super.dispose();
  }

  void _loadData(int a, int b) async {
    Response response1 = await getCourseScoreById(couId);
    // List responseData=json.decode(response.toString());
    print("###11" + response1.toString());
    setState(() {
      _courseScore = CourseScore.fromJson(json.decode(response1.toString()));
      print("###" + _remarkList.list.toString());
    });

    Response response = await getRemarkBycouIdType(a, b);
    // List responseData=json.decode(response.toString());
    print("###" + response.toString());
    setState(() {
      _remarkList = RemarkList.formJson(json.decode(response.toString()));
      print("###" + _remarkList.list.toString());
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );
    if (_remarkList.list.length == 0&&_courseScore==null) {
      return Container(
        width: 0,
        height: 0,
      );
    } else {
      _dataList = [
        EChartPieBean(title: "评价", number: 0, color: Colors.lightBlueAccent),
        EChartPieBean(title: "逻辑性强", number: _courseScore.scale[0], color: Colors.lightBlueAccent),
        EChartPieBean(title: "讲解细致", number: _courseScore.scale[1], color: Colors.deepOrangeAccent),
        EChartPieBean(title: "性价比高", number: _courseScore.scale[2], color: Colors.green),
        EChartPieBean(title: "内容丰富", number: _courseScore.scale[3], color: Colors.amber),
        EChartPieBean(title: "通俗易懂", number: _courseScore.scale[4], color: Colors.orange),
      ];
      return Container(
        width: double.infinity,
        height: double.infinity,
        padding: EdgeInsets.only(
            left: ScreenUtil().setWidth(55), right: ScreenUtil().setWidth(55)),
        child: Column(
          children: [
            SizedBox(height: ScreenUtil().setHeight(30)),
            Container(
              margin: EdgeInsets.only(
                  right: ScreenUtil().setWidth(100),
                  bottom: ScreenUtil().setHeight(116)),
              width: ScreenUtil().screenWidth,
              height: ScreenUtil().setHeight(422),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  SizedBox(
                    width: ScreenUtil().setWidth(255),
                    height: ScreenUtil().setWidth(395),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '综合评分',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(50),
                            color: Colors.black,
                          ),
                        ),
                        Text(
                          '${_courseScore.score}',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(143),
                            color: Colors.black,
                            //fontWeight: FontWeight.bold,
                            fontStyle: FontStyle.italic,
                          ),
                        ),
                        Row(
                          children: [
                            Icon(Icons.star_rounded,
                                size: ScreenUtil().setSp(50)),
                            Icon(Icons.star_rounded,
                                size: ScreenUtil().setSp(50)),
                            Icon(Icons.star_rounded,
                                size: ScreenUtil().setSp(50)),
                            Icon(Icons.star_rounded,
                                size: ScreenUtil().setSp(50)),
                            Icon(Icons.star_rounded,
                                size: ScreenUtil().setSp(50)),
                          ],
                        ),
                        Text(
                          '${_courseScore.peopleNum} 人已评价',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(40),
                            color: AppColors.subtext,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    width: ScreenUtil().setHeight(422),
                    height: ScreenUtil().setHeight(422),
                    child: Stack(
                      alignment: Alignment.center,
                      children: [
                        Container(
                          //来个高度
                          height: 0.8 * ScreenUtil().setHeight(422),
                          //宽度填充
                          width: 0.8 * ScreenUtil().setHeight(422),
                          //设置一下背景
                          color: Colors.white,
                          //封装一个方法构建左右排列的
                          child: buildPieChatWidget(),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Align(
              alignment: Alignment.bottomLeft,
              child: Text(
                '用户评价',
                style: TextStyle(
                  fontSize: ScreenUtil().setSp(47),
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Row(
              children: [
                InkWell(
                  onTap: () {
                    setState(() {
                      _loadData(couId, 1);
                      _list[_selectedIndex] = 0;
                      _selectedIndex = 0;
                      _list[_selectedIndex] = 1;
                    });
                  },
                  child: Chip(
                    label: Text(
                      '全部',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(45),
                        color: _list[0] == 1 ? Colors.white : Colors.black,
                      ),
                    ),
                    backgroundColor:
                        _list[0] == 1 ? AppColors.primary : AppColors.unactive,
                  ),
                ),
                SizedBox(width: ScreenUtil().setWidth(44)),
                InkWell(
                  onTap: () {
                    setState(() {
                      _loadData(couId, 2);
                      _list[_selectedIndex] = 0;
                      _selectedIndex = 1;
                      _list[_selectedIndex] = 1;
                    });
                  },
                  child: Chip(
                    label: Text(
                      '最新',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(45),
                        color: _list[1] == 1 ? Colors.white : Colors.black,
                      ),
                    ),
                    backgroundColor:
                        _list[1] == 1 ? AppColors.primary : AppColors.unactive,
                  ),
                ),
                SizedBox(width: ScreenUtil().setWidth(44)),
                InkWell(
                  onTap: () {
                    setState(() {
                      _loadData(couId, 3);
                      _list[_selectedIndex] = 0;
                      _selectedIndex = 2;
                      _list[_selectedIndex] = 1;
                    });
                  },
                  child: Chip(
                    label: Text(
                      '好评',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(45),
                        color: _list[2] == 1 ? Colors.white : Colors.black,
                      ),
                    ),
                    backgroundColor:
                        _list[2] == 1 ? AppColors.primary : AppColors.unactive,
                  ),
                ),
                SizedBox(width: ScreenUtil().setWidth(44)),
                InkWell(
                  onTap: () {
                    setState(() {
                      _loadData(couId, 4);
                      _list[_selectedIndex] = 0;
                      _selectedIndex = 3;
                      _list[_selectedIndex] = 1;
                    });
                  },
                  child: Chip(
                    label: Text(
                      '中评',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(45),
                        color: _list[3] == 1 ? Colors.white : Colors.black,
                      ),
                    ),
                    backgroundColor:
                        _list[3] == 1 ? AppColors.primary : AppColors.unactive,
                  ),
                ),
                SizedBox(width: ScreenUtil().setWidth(44)),
                InkWell(
                  onTap: () {
                    setState(() {
                      _loadData(couId, 5);
                      _list[_selectedIndex] = 0;
                      _selectedIndex = 4;
                      _list[_selectedIndex] = 1;
                    });
                  },
                  child: Chip(
                    label: Text(
                      '差评',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(45),
                        color: _list[4] == 1 ? Colors.white : Colors.black,
                      ),
                    ),
                    backgroundColor:
                        _list[4] == 1 ? AppColors.primary : AppColors.unactive,
                  ),
                ),
              ],
            ),
            //_buildRemark(_remarkList.list);
            Expanded(
              key: ValueKey(_remarkList),
              child: ListView.builder(
                itemCount: _remarkList.list.length,
                scrollDirection: Axis.vertical,
                itemBuilder: (BuildContext context, int index) {
                  return new RemarkItem(remark: _remarkList.list[index]);
                },
              ),
            ),
          ],
        ),
      );
    }
  }

  PieChatWidget buildPieChatWidget() {
    return PieChatWidget(
      dataList: _dataList,
      //是否输出日志
      isLog: false,
      //是否需要背景
      isBackground: true,
      //是否画直线
      isLineText: true,
      //背景
      bgColor: Colors.white,
      //是否显示最前面的内容
      isFrontgText: true,
      //默认选择放大的块
      initSelect: 0,
      //初次显示以动画方式展开
      openType: OpenType.ANI,
      //旋转类型
      loopType: LoopType.NON,
      //点击回调
      clickCallBack: (int value) {
        print("当前点击显示 $value");
      },
    );
  }

}
